var name = null;
var floo=null;
var roo=null;
var id=null;


var theData = new Vue({
    el: "#wrap",
    data: {
        nowPage: 1,   //当前页码
        totalPage: 0,  //总页码
        paginationItems: [   //页面数组
            /*{page:{}}*/  //数据格式
        ],
        tableItems: [       //表格数组
            /*{info:{}}*/  //数据格式
        ],
        flooritem:[

        ],
        roomtypeitem:[

        ],
        setUp_user: {},     //配置的用户
        search_btn_remark: "",    //存放用户搜索内容
        token: token,        //input file 的加密
        time: unixtime       //input file 的加密
        ,
        my_floor:""
    },
    methods: {
        getList: function (nextPage) {   //获取每个分页的数据
            var that = this;
            var search = "";
            if (that.search_btn_remark != "") {
                search = that.search_btn_remark;
            }
            $.ajax({
                url: 'api/room/get_room_list.php',
                type: 'POST',
                async:false,
                data: {
                    token: token,
                    time: unixtime,
                    nextPage: nextPage,
                    search: search
                },
                dataType: 'JSON',
                beforeSend: function () {
                    $("#loading-mask").show();
                },
                error: function () {
                    $("#loading-mask").hide();
                    MyAlert("请求超时");
                },
                success: function (res) {
                    $("#loading-mask").hide();
                    if (res.err == 0) {
                        that.tableItems = res.msg;
                        that.nowPage = nextPage;
                    } else {
                        MyAlert(res.msg);
                    }
                }
            })
        },
        getPagination: function () {    //获取分页数量
            var that = this;
            var search = "";
            if (that.search_btn_remark != "") {
                search = that.search_btn_remark;
            }
            $.ajax({
                url: 'api/room/room_pag_info.php',
                type: 'POST',
                async:false,
                data: {
                    token: token,
                    time: unixtime,
                    search: search,
                },
                dataType: 'JSON',
                beforeSend: function () {
                    $("#loading-mask").show();
                },
                error: function () {
                    $("#loading-mask").hide();
                    MyAlert("分页请求超时");
                },
                success: function (res) {
                    $("#loading-mask").hide();
                    if (res.err == 0) {
                        console.log(res.msg);
                        that.paginationItems = res.msg;
                        that.totalPage = res.total_page;
                    } else {
                        MyAlert(res.msg);
                    }
                }
            })
        },
        search: function () {      //查询
            var regSpace = /^\s+$/;
            if ($("#search-text").val() && !regSpace.test($("#search-text").val())) {
                this.search_btn_remark = $("#search-text").val();
                this.getPagination();
                this.getList(1);
            } else {
                MyAlert("请输入搜索内容");
            }
        },

        getfloor:function(){//获取楼宇信息
            var that = this;
            $.ajax({
                url: 'api/room/get_floor.php',
                type: 'POST',
                async:false,
                data: {
                    token: token,
                    time: unixtime,
                },
                dataType: 'JSON',
                beforeSend: function () {
                    $("#loading-mask").show();
                },
                error: function () {
                    $("#loading-mask").hide();
                    MyAlert("分页请求超时");
                },
                success: function (res) {
                    $("#loading-mask").hide();
                    if (res.err == 0) {
                        that.flooritem = res.msg;
                    } else {
                        MyAlert(res.msg);
                    }
                }
            })
        },

        getroomtype:function(){//获取房间类型信息
            var that = this;
            $.ajax({
                url: 'api/room/get_room_type.php',
                type: 'POST',
                async:false,
                data: {
                    token: token,
                    time: unixtime,
                },
                dataType: 'JSON',
                beforeSend: function () {
                    $("#loading-mask").show();
                },
                error: function () {
                    $("#loading-mask").hide();
                    MyAlert("分页请求超时");
                },
                success: function (res) {
                    $("#loading-mask").hide();
                    if (res.err == 0) {
                        that.roomtypeitem = res.msg;
                    } else {
                        MyAlert(res.msg);
                    }
                }
            })
        },

        popAddRoom:function(){//添加房间信息弹窗
            $("#pop-add-room").modal();
        },

        popAddRoomSubmit:function (){//添加房间信息提交
            var that = this;
            var name = $("#pop-add-room-name").val();
            var floor = $("#pop-add-room-floor option:selected").text();
            var type = $("#pop-add-room-type option:selected").text();
            var foor_id = null;
            var type_id = null;
            if(name!="" && floor!="" &&type!=""){
                for(var i=0;i<that.flooritem.length;i++){//对比选择的楼宇名称获取楼宇ID
                    if(floor===that.flooritem[i].info.floor_name){
                        foor_id= that.flooritem[i].info.floor_id;
                    }
                }
                for(var i=0;i<that.roomtypeitem.length;i++){//对比选择的房间类型名称获取房间类型ID
                    if(type===that.roomtypeitem[i].info.room_type){
                        type_id= that.roomtypeitem[i].info.id;
                    }
                }
                if(type_id==null)
                {
                    type_id=type;
                }
                if(foor_id==null){
                    foor_id=floor;
                }
                $.ajax({
                    url: 'api/room/add_room.php',
                    type: 'POST',
                    data: {
                        token: token,
                        time: unixtime,
                        room_name:name,
                        romm_type:type_id,
                        floor_id:foor_id,
                    },
                    dataType: 'JSON',
                    beforeSend: function () {
                        $("#loading-mask").show();
                    },
                    error: function () {
                        $("#loading-mask").hide();
                    },
                    success: function (res) {
                        $("#loading-mask").hide();
                        if (res.err == 0) {
                            MyAlert(res.msg, function () {
                                window.location.reload();
                            });

                        } else {
                            MyAlert(res.msg);
                        }
                    }
                })
            }
            else{
                MyAlert("请填写必填信息");
            }
        },

        popUpdataRoom:function(item){//修改房间信息弹窗
            var that=this;
            $("#pop-updata-room-name").val(item.info.room_name);
            $("#pop-updata-room-floor").val(item.info.floor_id);
            //console.log(item);
            that.my_floor=item.info.floor_id;
            $("#pop-updata-room-type").val(item.info.type_name);
            $("#pop-updata-room").modal();
            name = item.info.room_name;
            floo = item.info.floor_id;
            roo = item.info.type_name;
            id=item.info.id;

        },

        popUpadataRoomSubmit:function(){//修改房间信息提交

            var that=this;
            var roomname = $("#pop-updata-room-name").val();
            var floorid = $('#pop-updata-room-floor option:selected').text();
            var roomtype = $('#pop-updata-room-type option:selected').text();
            var belongfloor=null;
            var belongroomtype = null;
            if(roomname!=""||floorid!=""||roomtype!=""){
                if(name == roomname && floorid==floo && roomtype==roo){
                    MyAlert("信息未修改");
                }
                else{
                    for(var i=0;i<that.flooritem.length;i++){//对比选择的楼宇名称获取楼宇ID
                        if(floorid===that.flooritem[i].info.floor_name){
                            belongfloor= that.flooritem[i].info.floor_id;
                        }
                    }
                    for(var i=0;i<that.roomtypeitem.length;i++){//对比选择的房间类型名称获取房间类型ID
                        if(roomtype===that.roomtypeitem[i].info.room_type){
                            belongroomtype = that.roomtypeitem[i].info.id;
                        }
                    }
                    if(belongfloor==null)
                    {
                        belongfloor =floorid;
                    }
                    if(belongroomtype==null){
                        belongroomtype =roomtype ;
                    }
                    MyConfirm("确定修改?", function (){
                        $.ajax({
                            url: "api/room/updataRoomAuthority.php",
                            type: 'POST',
                            data: {
                                token: token,
                                time: unixtime,
                                room_name:roomname,
                                floor_id:belongfloor,
                                id:id,
                                type_id:belongroomtype,
                            },
                            dataType: 'JSON',
                            beforeSend: function () {
                                $("#loading-mask").show();
                            },
                            error: function () {
                                $("#loading-mask").hide();
                                alert("请求超时");
                            },
                            success: function (res) {
                                $("#loading-mask").hide();
                                if (res.err == 0) {
                                    MyAlert(res.msg, function () {
                                        window.location.reload();
                                    });
                                } else {
                                    MyAlert(res.msg);
                                }
                            }
                        })
                    })
                }
            }
            else{
                MyAlert("请填写必填信息");
            }
        },

        deleteRoomAuthority:function(index){//删除房间信息
            MyConfirm("确定删除?", function () {
                $.ajax({
                    url: "api/room/deleteRoomInfo.php",
                    type: 'POST',
                    data: {
                        token: token,
                        time: unixtime,
                        index: index
                    },
                    dataType: 'JSON',
                    beforeSend: function () {
                        $("#loading-mask").show();
                    },
                    error: function () {
                        $("#loading-mask").hide();
                        alert("请求超时");
                    },
                    success: function (res) {
                        $("#loading-mask").hide();
                        if (res.err == 0) {
                            MyAlert(res.msg, function () {
                                window.location.reload();
                            });
                        } else {
                            MyAlert(res.msg);
                        }
                    }
                })
            })
        },


        aaa:function () {//单击显示房间人员信息
            var that= this;
            console.log(that.tableItems);
            console.log(that.flooritem);
        }



    }
});

theData.getroomtype();//首次加载获取房间类型信息
theData.getfloor();//首次加载获取楼宇信息
theData.getPagination();   //首次加载总页码
theData.getList(theData.nowPage);  //首次加载首个分页